﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Microsoft.Phone.Shell;
using WP8Demos.Helpers;

namespace WP8Demos.ViewModels
{
    public class LockViewModel
    {
        private ICommand settingsButtonCommand;
        private string notificationNumber;

        public LockViewModel()
        {
        }

        public ICommand SettingsButtonCommand
        {
            get
            {
                if (settingsButtonCommand == null)
                {
                    settingsButtonCommand = new RelayCommand(SettingsButtonCommandTapped);
                }
                return settingsButtonCommand;
            }
        }


        public string NotificationNumber
        {
            get { return notificationNumber; }
            set
            {
                if (notificationNumber != value)
                {
                    notificationNumber = value;
                    TryUpdateLockScreenNotificationsNumber();
                }
            }
        }

        private void TryUpdateLockScreenNotificationsNumber()
        {
            int newCount = 0;
            int.TryParse(notificationNumber, out newCount);
            UpdateLockScreenNotificationsNumber(newCount);
        }

        private void UpdateLockScreenNotificationsNumber(int newCount)
        {
            var firstTile = ShellTile.ActiveTiles.FirstOrDefault();
            firstTile.Update(new CycleTileData()
            {
                Count = newCount,
                CycleImages = new List<Uri>()
                {
                    new Uri("Assets\\CycleTiles\\1.png", UriKind.Relative),
                    new Uri("Assets\\CycleTiles\\2.png", UriKind.Relative),
                    new Uri("Assets\\CycleTiles\\3.png", UriKind.Relative),
                    new Uri("Assets\\CycleTiles\\4.png", UriKind.Relative),
                    new Uri("Assets\\CycleTiles\\5.png", UriKind.Relative),
                    new Uri("Assets\\CycleTiles\\6.png", UriKind.Relative),
                    new Uri("Assets\\CycleTiles\\7.png", UriKind.Relative),
                    new Uri("Assets\\CycleTiles\\8.png", UriKind.Relative),
                    new Uri("Assets\\CycleTiles\\9.png", UriKind.Relative),
                },
            });
        }

        private async void SettingsButtonCommandTapped()
        {
            await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings-lock:"));
        }
    }
}
